package com.zr.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.zr.dao.NewlyIincreasedStaffDao;
import com.zr.model.LMdistributionPerson;
import com.zr.model.LMuser;
import com.zr.service.NewlyIincreasedStaffService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Repository(value="NStaffservice")
public class NewlyIincreasedStaffServiceImpl implements NewlyIincreasedStaffService {
	@Resource
	private NewlyIincreasedStaffDao<LMdistributionPerson> NStaffdao;
	


	@Override
	public JSONObject getStaffByUid(int rows, int page, int uid) {
		// TODO Auto-generated method stub
		JSONObject staffJO = new JSONObject();
		int total = NStaffdao.findNumStaff(uid);
		staffJO.put("total", total);
		List<LMdistributionPerson> lm = NStaffdao.findStaff(rows, page, uid);
		JsonConfig   cfg = new JsonConfig();
		cfg.setExcludes(new String[]{"LMuser"});
		staffJO.put("rows",JSONArray.fromObject(lm, cfg));
		return staffJO;
	}

	@Override
	public int addStaff(String dpname,String dpage,String dpphone,LMuser LMuser) {
		// TODO Auto-generated method stub
		int flag = 0;
		if(dpage==null&&dpname==null&&dpphone==null&&LMuser.getUid()==0){
			return 0;
		}
		StringBuffer hql = new StringBuffer("from LMdistributionPerson lmp where ");
		LMdistributionPerson lmp = new LMdistributionPerson();
		lmp.setDpage(dpage);
		lmp.setDpname(dpname);
		lmp.setDpphone(dpphone);
		lmp.setLMuser(LMuser);
		hql.append("dpphone="+dpphone);
		List<LMdistributionPerson> l = NStaffdao.find(hql.toString());
		if(l.size()>0){
			flag = -1;
		}else{
			NStaffdao.save(lmp);
			flag = 1;
		}
		
		return flag;
	}

	@Override
	public boolean modifyStaff(LMdistributionPerson lmp) {
		// TODO Auto-generated method stub
		String hql = "from LMdistributionPerson lm where lm.phone="+lmp.getDpphone()+" and lm.dpid not "+lmp.getDpid();
		if(NStaffdao.find(hql).size()>0){
			return false;
		}
		NStaffdao.update(lmp);
		return true;
	}

	@Override
	public boolean deleteStaffById(int[] dpids) {
		// TODO Auto-generated method stub
		boolean flag = false;
		System.out.println(dpids);
		if(dpids!=null){
			for (int i = 0; i < dpids.length; i++) {
				LMdistributionPerson lmp = new LMdistributionPerson();
				lmp.setDpid(dpids[i]);
				NStaffdao.delete(lmp);
				flag  = true;
			}
		}else{
			flag = false;
		}
		return flag;
	}



}
